package aidturith.io;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

public class Writer {

	public final static String DEFAULT_ENCODING = "UTF8";
	
	public static void write_line(String text, String file, boolean append) throws IOException {
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), DEFAULT_ENCODING));
		
		out.append(text);
		out.newLine();
		out.flush();
		out.close();
	}
	
	public static void write_line(String text, String file, String encoding, boolean append) throws IOException {
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), encoding));
		
		out.append(text);
		out.newLine();
		out.flush();
		out.close();
	}
	
	public static void write_line(Set<String> text, String file, boolean append) throws IOException {
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), DEFAULT_ENCODING));
		
		for(String str : text) {
			out.append(str);
			out.newLine();
		}
			
		out.flush();
		out.close();
	}
	
	public static void write_line(Set<String> text, String file, String encoding, boolean append) throws IOException {
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), encoding));
		
		for(String str : text) {
			out.append(str);
			out.newLine();
		}
			
		out.flush();
		out.close();
	}
	
	
	// tests
	public static void main(String[] args) {
		Set<String> text = new HashSet<String>();
		
		for(int i=0; i<10; i++)
			text.add("this is a line!"+i);
		
		try {
			Writer.write_line(text, "C:\\Users\\Aidturith\\test.txt", true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
